package flybrid;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class BirdGame extends JPanel {
    BufferedImage background;
    BufferedImage startimage;
    BufferedImage gameoverimage;
    Ground ground;
    Column column1,column2;
    brid brid;
    int score;
    int state;
    public static final int START=0;
    public static final int RUNNING=1;
    public static final int OVER=2;

    public BirdGame() throws Exception {
        background= ImageIO.read(getClass().getResource( "bg.png"));
        startimage=ImageIO.read(getClass().getResource("start.png"));
        gameoverimage=ImageIO.read(getClass().getResource("gameover.png"));
        ground=new Ground();
        column1=new Column(1);
        column2=new Column(2);
        brid=new brid();
        score=0;
        state=START;

    }

    public void step(){
        try {
            switch (state){
                case START:
                    state=RUNNING;
                    break;
                case RUNNING:
                    brid.flappy();
                    break;
                case OVER:
                    column1=new Column(1);
                    column2=new Column(2);
                    brid=new brid();
                    score=0;
                    state=START;
                    break;
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    public void action()throws Exception{
        KeyListener k=new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_K)score+=10;
                if(e.getKeyCode()==KeyEvent.VK_SPACE){
                    step();
                }
            }
        };
        MouseListener l=new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                step();
            }
        };
        requestFocus();
        addKeyListener(k);
        addMouseListener(l);
        while (true){
            switch (state)
            {
                case START:
                    brid.fly();
                    ground.step();
                    break;
                case RUNNING:
                    ground.step();
                    column1.step();
                    column2.step();
                    brid.fly();
                    brid.step();
                    if(brid.x==column1.x||brid.x==column2.x)score++;
                    if(brid.hit(ground)||brid.hit(column1)||brid.hit(column2))state=OVER;
                    break;
            }
            repaint();
            Thread.sleep(1000/60);
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(background,0,0,null) ;
        g.drawImage(ground.image,ground.x,ground.y,null);
        g.drawImage(column1.image,column1.x-column1.whdth/2,
                column1.y-column1.height/2,null);
        g.drawImage(column2.image,column2.x-column2.whdth/2,
                column2.y-column2.height/2,null);
        Graphics2D g2=(Graphics2D)g;
        g2.rotate(-brid.alpha,brid.x,brid.y);
        g.drawImage(brid.image,brid.x-brid.width/2,
                brid.y-brid.height/2,null);
        g2.rotate(brid.alpha,brid.x,brid.y);
        Font f=new Font(Font.SANS_SERIF,Font.BOLD,40);
        g.setFont(f);
        g.drawString(""+score,40,60);
        g.setColor(Color.WHITE);
        g.drawString(""+score,40-3,60-3);
        switch (state){
            case START:
                g.drawImage(startimage,0,0,null);
                break;
            case OVER:
                g.drawImage(gameoverimage,0,0,null);
                break;
        }
    }

    public static void main(String[] args) throws Exception{
        JFrame frame=new JFrame();
        BirdGame birdGame=new BirdGame();
        frame.setTitle("飞翔的小鸟");
        frame.add(birdGame);
        frame.setSize(440,670);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        birdGame.action();
    }
}
